<?php
// 模块名_控制名
class Order_WaitshipmentController extends Zend_Controller_Action {
	private $_s;
	public function preDispatch() {
		$this->_s = new Order_Service_Order ();
		$this->tplDirectory = "order/waitshipment/"; // 控制对应模板目录
	}
	
	public function indexAction() {
		
		$value = '1';
		$field = 'st_status';
		$shipTypeArr = Shipping_Service_ShipType::getAllByValue ( $value, $field );
		$orderStatus = Order_Service_Order::getAllOrderStatus();
		
		$conShipMethod=array(
				"orders_status"=>5,
				"process_status"=>5
				);
		$shipMethodOrderby="shipping_method asc ";
		
		$shipModel=new Order_Model_Waitshipment();
		
		$countShipMethodRow=$shipModel->groupByShipMethod($conShipMethod,$shipMethodOrderby);
		$shipCount=0;
		if($countShipMethodRow){
			foreach ($countShipMethodRow as $val){
				$shipCount+=$val['count'];
			}
		}
		//print_r($countShipMethodRow);exit;
		
		$this->view->countShipMethodRow=$countShipMethodRow;
		$this->view->shipCount=$shipCount;
		$this->view->shipTypeArr = $shipTypeArr;
		$this->view->orderStatus = $orderStatus;
		
		echo Cff::renderTpl ( $this->tplDirectory . "index.tpl", "layout" );
	}
	
	public function updateAction() {
		$data = array ();
		$orders_shipment = $this->_request->getParam ( 'orders_shipment', '' );
        $tracking_number = $this->_request->getParam ( 'tracking_number', '' );
		$os_weight = $this->_request->getParam ( 'os_weight', '0' );
		
		$os_length = $this->_request->getParam ( 'txt_length', '0' );
		$os_width = $this->_request->getParam ( 'txt_width', '0' );
		$os_height = $this->_request->getParam ( 'txt_height', '0' );
		
		// 查看订单是否存在
		$row_order = Order_Service_Order::getByOrdersCode ( $orders_shipment );

		if(!$row_order){
			$data = array ('ask' => -1,'error' => $this->lang['pls_input_order_right_code']);
			die ( Zend_Json::encode ( $data ) );
		}
		if(empty($tracking_number)){
            $data = array ('ask' => -1,'error' => $this->lang['pls_input_ship_code']);
            die ( Zend_Json::encode ( $data ) );
        }
		
		// 查看是否是待发货状态
		if($row_order['process_status']!=4){
			$data = array ('ask' => -1,'error' => $this->lang['order_status_wrong_can_not_ship']);
			die ( Zend_Json::encode ( $data ) );
		}
		
		// 查看是否是异常状态的单:1库存不够,2余额不足,3.已被截单

		switch ($row_order['underreview_status']){
			case 3:
			   $data = array ('ask' => -1,'error' => $this->lang['order_stop_can_not_ship']);
			   die ( Zend_Json::encode ( $data ) );
			   break;
			case 2:
			   $data = array ('ask' => -1,'error' => $this->lang['order_balance_insufficient_can_not_ship']);
			   die ( Zend_Json::encode ( $data ) );
			break;
			case 1:
				$data = array ('ask' => -1,'error' => $this->lang['order_inventory_insufficient_can_not_ship']);
			   die ( Zend_Json::encode ( $data ) );
			   break;
			
		}
		
		
		// 客户ID
		$customer_id = $row_order ['customer_id'];
		//
		// 更新回填重量
		$update_os_wp = array ('weight' => $os_weight, 
				'orderCode' => $orders_shipment, 
				'length'=>$os_length,
				'width'=>$os_width,
				'height'=>$os_height,
				'ignoreWeight' => false,
                'tracking_number'=>$tracking_number
        )

		;
		
		$update_result = new Order_Service_DispatchProccess ( $update_os_wp );
		
		
		if (! $update_result->DispatchTransaction ()) {
			// 判断重量是否匹配
			if (! $update_result->_isWeight) {
				$data = array ('ask' => 2, 
						'shipmode' =>  $update_result->getshippingMethod(), 
						'volWeight'=>$update_result->getshipType(),
						'volWeightRate'=>$update_result->getvolWeightRate(),
						'error' => $update_result->getError () );
				die ( Zend_Json::encode ( $data ) );
			} else {
				$data = array ('ask' => 0,
						'shipmode' =>  $update_result->getshippingMethod(), 
						'volWeight'=>$update_result->getshipType(),
						'volWeightRate'=>$update_result->getvolWeightRate(),
						'error' => $update_result->getError () );

				die ( Zend_Json::encode ( $data ) );
			}
		
		} else {
			$data = array ('ask' => 1, 
					'shipmode' =>  $update_result->getshippingMethod(),
					'volWeight'=>$update_result->getshipType(), 
					'volWeightRate'=>$update_result->getvolWeightRate(),
					'error' => $update_result->getError () );
		}
		
		die ( Zend_Json::encode ( $data ) );
	
	}
	
	// 忽略重量进行提交
	public function ignoreweightupdateAction() {
		$data = array ();
		$orders_shipment = $this->_request->getParam ( 'orders_shipment', '' );
        $tracking_number = $this->_request->getParam ( 'tracking_number', '' );
		$os_weight = $this->_request->getParam ( 'os_weight', '0' );
		$os_length = $this->_request->getParam ( 'txt_length', '0' );
		$os_width = $this->_request->getParam ( 'txt_width', '0' );
		$os_height = $this->_request->getParam ( 'txt_height', '0' );
		
		// 查看订单是否存在
		$row_order = Order_Service_Order::getByOrdersCode ( $orders_shipment );
        if(!$row_order){
            $data = array ('ask' => -1,'error' => $this->lang['pls_input_order_right_code']);
            die ( Zend_Json::encode ( $data ) );
        }
        if(empty($tracking_number)){
            $data = array ('ask' => -1,'error' => $this->lang['pls_input_ship_code']);
            die ( Zend_Json::encode ( $data ) );
        }
		// 客户ID
		$customer_id = $row_order ['customer_id'];
		//
		// 更新回填重量
		$update_os_wp = array ('weight' => $os_weight, 
				'orderCode' => $orders_shipment, 
				'length'=>$os_length,
				'width'=>$os_width,
				'height'=>$os_height,
				'ignoreWeight' => true, 'tracking_number'=>$tracking_number )

		;
		
		$update_result = new Order_Service_DispatchProccess ( $update_os_wp );
		
		if (! $update_result->DispatchTransaction ()) {
			$data = array ('ask' => 0, 'error' => $update_result->getError() );
			die ( Zend_Json::encode ( $data ) );
		
		} else {
			$data = array ('ask' => 1, 'shipmode' => $row_order ['shipping_method'], 'error' => $update_result->getError () );
		
		}
		
		die ( Zend_Json::encode ( $data ) );
	
	}
	
	public function listAction() {
		$intpage = $this->_request->getParam ( 'page', 1 );
		$pageSize = $this->_request->getParam ( 'pageSize',5 );
		$dateFrom = trim ( $this->_request->getParam ( "dateFrom", "0" ) );
		$dateTo = trim ( $this->_request->getParam ( "dateTo", "0" ) );
		$process_status = 4;
		$payment_status = $this->_request->getParam ( 'payment_status', '' );
		$warehouse_id = $this->_request->getParam ( 'warehouse_id', '' );
		$shipping_method = $this->_request->getParam ( 'st_name', '' );
		
		$browsetype = $this->_request->getParam ( 'browsetype', 'fold' ); // 收展产品
		
		$intpage = ($intpage < 1) ? 1 : $intpage;
		$condition = array (
				'process_status' => $process_status,
				 'payment_status' => $payment_status, 
				'warehouse_id' => $warehouse_id, 
				'shipping_method' => $shipping_method, 
				"order_date_from" => $dateFrom, 
				"order_date_to" => $dateTo 
				
				);
		
		$count = $this->_s->getByCondition ( $condition, 'count(*)', '', 0, 0 );
		$data = array ('count' => 0, 'ask' => 0 );
		if ($count > 0) {
			$Rows = $this->_s->getByCondition ( $condition, '*', 'shipping_method desc', $pageSize, $intpage );
			foreach ( $Rows as $key => $value ) {
				$Rows [$key] ['orders_status'] = $this->_s->getProcessStatus ( $value ['process_status'] );
				if ($browsetype != 'fold') {
					$order_product = Order_Service_Product::getByCondition ( array ('orders_code' => $value ['orders_code'] ) );
					foreach ( $order_product as $k => $v ) {
						$w_product = Warehouse_Service_Products::getByProductId ( $v ['product_id'] );
						$order_product [$k] ['ws_code'] = $w_product ['ws_code'];
					}
					$Rows [$key] ['order_product'] = $order_product;
				}
				$country = Common_Service_Country::getByValue ( $value ['consignee_country_id'], 'country_id' );
				$Rows [$key] ['consignee_country_id'] = $country ['country_code'];
			}
			$data = array ('count' => $count, 'result' => $Rows, 'ask' => 1 );
		}
		die ( Zend_Json::encode ( $data ) );
	
	}

}